import 'package:flutter/material.dart';
import 'package:xws/models/goods/goods_obj.dart';
import 'package:xws/utils/app_util.dart';
import 'package:xws/utils/data_util.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/widgets/app_img.dart';
import 'package:xws/widgets/app_label.dart';
import 'package:xws/widgets/goods_config/goods_detail/detail_page.dart';

/// 物品项
class GoodsItem extends StatelessWidget {
  const GoodsItem({
    Key? key,
    required this.index,
    required this.goodsObj,
    this.fromUser = false,
    this.refresh,
  }) : super(key: key);

  final int index;
  final GoodsObj goodsObj;
  final bool fromUser;

  final Function()? refresh;

  @override
  Widget build(BuildContext context) {
    final GoodsObj g = goodsObj;

    return GestureDetector(
      onTap: () => push(context, DetailPage(goods: g, index: index, fromUser: fromUser, refresh: refresh)),
      child: SizedBox(
        height: index == 0 ? 200 : 280,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Expanded(
              child: AppImg.fitCover(
                url: g.photoUrlList.isNotEmpty ? g.photoUrlList.first : null,
                width: sw - 30,
                radius: 10,
              ),
            ),
            const SizedBox(height: 4),
            Text(
              g.name,
              style: TextStyle(
                fontWeight: FontWeight.bold,
                color: black(context).withOpacity(0.8),
                fontSize: 14,
              ),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
            const SizedBox(height: 6),
            Row(
              children: <Widget>[
                AppLabel(
                  content: getQualityValue(g.age),
                  color: theme(context),
                  margin: const EdgeInsets.only(right: 6),
                ),
                if (g.favoriteFlag)
                  const AppLabel(
                    content: '好友',
                    color: Colors.blueGrey,
                    margin: EdgeInsets.only(right: 6),
                  ),
                if (g.distance.isNotEmpty)
                  AppLabel(
                    content: g.distance,
                    color: Colors.blueGrey,
                  ),
                const Spacer(),
                AppLabel(
                  content: '免费',
                  color: Colors.green[600],
                ),
              ],
            ),
            const SizedBox(height: 6),
            Row(
              children: <Widget>[
                AppImg.fitCover(
                  url: g.user.avatarUrl,
                  width: 20,
                  height: 20,
                  radius: 10,
                ),
                const SizedBox(width: 6),
                Expanded(
                  child: Text(
                    g.user.nickName,
                    maxLines: 1,
                    style: TextStyle(
                      fontSize: 12,
                      color: black(context).withOpacity(0.7),
                    ),
                  ),
                ),
                Row(
                  children: <Widget>[
                    Icon(
                      Icons.location_on_rounded,
                      size: 15,
                      color: disable(context),
                    ),
                    Text(
                      g.region,
                      style: TextStyle(
                        fontSize: 12,
                        color: disable(context),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
